//
//  TSTCalendarPage.m
//  TST
//
//  Created by POP3mac on 2020/4/27.
//  Copyright © 2020 YTYanK. All rights reserved.
//

#import "TSTCalendarPage.h"
#import "LXCalender.h"
#import "ElasticBoxView.h"

@interface TSTCalendarPage ()
@property(nonatomic,strong)LXCalendarView *calenderView;
@property (nonatomic, strong)ElasticBoxView * box;

@property(strong, nonatomic) NSMutableArray * jobsLists;
@end

@implementation TSTCalendarPage

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
      [self setAllInitSubView];
      [self setAllSubViewLayout];
      [self updateAllData];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    
//    [self  timeFormatString:[TSTApi sharedTSTApi].userModel.add_time];
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [formatter stringFromDate: date];
    NSLog(@"----bbbb>%@",dateStr);
    [self  acquireDateString:dateStr];
    

////    NSLog(@"----bbbb>%@",[TSTApi sharedTSTApi].userModel.add_time);
//    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]*1000];
//    NSLog(@"**----》%@",timeSp);
}

#pragma mark - Navigation

// In a storyboard-based application, you will of en want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

- (void)setAllInitSubView {
    // 80
    self.calenderView =[[LXCalendarView alloc]initWithFrame:CGRectMake(20, SCREEN_NAV_BAR, Device_Width - 40, 0)];
    // 设置每个天数的颜色
    self.calenderView.currentMonthTitleColor =  UIColor.blackColor;
    self.calenderView.lastMonthTitleColor =[UIColor hexStringToColor:@"8a8a8a"];
    self.calenderView.nextMonthTitleColor =[UIColor hexStringToColor:@"8a8a8a"];

    self.calenderView.isHaveAnimation = YES;
    
    self.calenderView.isCanScroll = YES;
    self.calenderView.isShowLastAndNextBtn = YES;
    
    //当天
    self.calenderView.todayTitleColor =[UIColor blueColor];
    
    self.calenderView.selectBackColor =[UIColor redColor];
    
    
    self.calenderView.isShowLastAndNextDate = NO;
    
    [self.calenderView dealData];
    
    self.calenderView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.calenderView];
    __weak __typeof(self)weakSelf = self;
    self.calenderView.selectBlock = ^(NSInteger year, NSInteger month, NSInteger day) {
        NSLog(@"%ld年 - %ld月 - %ld日",year,month,day);
        NSString *_date = [NSString stringWithFormat:@"%ld-%ld-%ld",year,month,day];
        [weakSelf acquireDateString:_date];
    };
    
    
    
    self.isCustomTable = YES;
//    if (IS_IPHONE_X) {
//       }else {
//
//       }
    
    // 需要创建对象之后使用

    self.baseTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.calenderView.maxY, SCREEN_WIDTH, SCREEN_HEIGHT - self.calenderView.maxY)];
    self.baseTableView.mj_header = nil;
    self.baseTableView.mj_footer = nil;
    self.baseTableView.backgroundColor = YTYRGBA(242, 242, 242, 1);
    
//    self.baseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.baseTableView];
  
    
   self.additionalBlock = ^(UITableViewCell * _Nonnull cell, NSIndexPath * _Nonnull indexPath) {
           UIView *subView = [[UIView alloc] initWithFrame:CGRectZero];
           subView.backgroundColor = UIColor.whiteColor;
            

            UILabel *l0 = [[UILabel alloc] initWithFrame:CGRectZero];
            l0.textAlignment = NSTextAlignmentLeft;
            l0.textColor = UIColor.blackColor;
            l0.numberOfLines = 3;
            l0.font = [UIFont systemFontOfSize:13];
       
            GetJobsModel * model = weakSelf.jobsLists[indexPath.row];
            NSString *string  = [NSString stringWithFormat:@"%@\n%@\n%@",model.staff_name,model.job_title,model.status_str];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
       
            NSRange strRange = {[string length] - model.status_str.length,model.status_str.length};
            [str addAttribute:NSForegroundColorAttributeName value:UIColor.grayColor range:strRange];
              l0.attributedText = str;
             [subView addSubview:l0];

             
            

             [l0 mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.top.equalTo(subView).with.offset(0);
                 make.left.equalTo(subView).with.offset(20);
                 make.height.equalTo(subView.mas_height);
                 make.width.equalTo(subView.mas_width);  //mas_equalTo(leftW);
             }];

           [cell addSubview:subView];
           [subView mas_makeConstraints:^(MASConstraintMaker *make) {
               make.top.equalTo(cell).with.offset(0);
               make.left.equalTo(cell).with.offset(0);
               make.height.equalTo(cell);
               make.width.equalTo(cell);
           }];
       };

 
//        self.navigationItem.leftBarButtonItem = [YTYTools obtainBackItemWithTarget:self action:@selector(backClick) image:[[UIImage imageNamed:@"back.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic]];
    
    
}


- (void)setAllSubViewLayout {
    
}

- (void)updateAllData {
    
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.jobsLists.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}


//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    DeliveryReportCell *cell = [DeliveryReportCell initCellWithCellView:tableView reuseIdentifier:@"DeliveryReportCell"];
//    cell.nestingTableView = tableView;
//
//    cell.icon.image = [UIImage imageNamed:@"logo"];
//    cell.icon.backgroundColor = UIColor.redColor;
//    cell.number.text = @"員工編號 382846583";
//    cell.name.text = @"名稱\n陳大文";
//    [cell.name setRangeOfString:@"\n" lineSpacing:6 firstFont:[UIFont systemFontOfSize:12] firstColor:YTYRGBA(81, 165, 216, 1) tailFont:[UIFont systemFontOfSize:13] tailColor:UIColor.blackColor];
//
//    return  cell;
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       UIWindow *window = nil;
        if (@available(iOS 13.0, *)) {
          for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes)
          {
                  if (windowScene.activationState == UISceneActivationStateForegroundActive)
                  {
                      window = windowScene.windows.firstObject;
                      break;
                  }
          }
        }else{
         window = [[UIApplication sharedApplication] delegate].window;
        }
    GetJobsModel *jobs = self.jobsLists[indexPath.row];
    NSLog(@"--》〉%@", jobs.jobs_id);
    
    [TSTApi requestWithUrl:AUJobView parameters:@{@"job_id":jobs.jobs_id} method:NetMethodGET returnModel:^(id  _Nonnull model) {
        
        [TSTApi sharedTSTApi].jobsViewModel = [JobsViewModel yy_modelWithDictionary:model];

        self.box = [[ElasticBoxView alloc] initWithFrame:CGRectZero];
        self.box.backgroundColor = YTYRGBA(0, 0, 0, 0.5);

        UITapGestureRecognizer * tap = [YTYTools obtainTapGestureRecognizerObjectWithAction:@selector(onClick) withTarget:self];
        [self.box addGestureRecognizer:tap];
        [UIView transitionWithView:self.box duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{

        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        [window  addSubview:self.box];

        [self.box mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(window).with.offset(0);
            make.centerX.equalTo(window);
            make.height.equalTo(window);
            make.width.equalTo(window);
        }];

        [UIView setAnimationsEnabled:oldState];
        } completion:nil];
        
        
    } returnError:^(id  _Nonnull error) {
        [MBProgressHUD  showError:error];
    }];
    
    
    
        
 }

-  (void)onClick{
    [self.box removeFromSuperview];
}

// 请求接口
- (void)acquireDateString:(NSString *)dateString {
    NSMutableDictionary *mutdic = [NSMutableDictionary dictionary];
      [mutdic setObject:[TSTApi sharedTSTApi].userModel.staff_id forKey:@"staff_id"];
      [mutdic setObject:dateString forKey:@"date"];
    [TSTApi requestWithUrl:AUGetJobs parameters:mutdic method:NetMethodGET returnModel:^(id  _Nonnull model) {
        
        self.jobsLists = (NSMutableArray *)[NSMutableArray yy_modelArrayWithClass:[GetJobsModel class] json:model];
        [self.baseTableView reloadData];
    } returnError:^(id  _Nonnull error) {
        [MBProgressHUD  showError:error];
    }];
}


#pragma mark - 加载刷新
// 刷新
- (void)tableRefreshData {
    [self tableRefreshDataWithPage:1];
}
- (void)tableLoadData {
    [self tableLoadDataWithPage:1];
}

// 刷新
- (void)tableRefreshDataWithPage:(int)page {
    [self.baseTableView.mj_header endRefreshing];
    [self.baseTableView reloadData];
}

// 加载
- (void)tableLoadDataWithPage:(int)page {
    [self.baseTableView.mj_footer endRefreshing];
}
// 时间戳 转字符串时间
- (NSString *)timeFormatString:(NSString *)dateString {
    NSTimeInterval interval = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];

    NSString *dateStr = [formatter stringFromDate: date];
    
    return dateStr;
}


@end
